package com.yunfei.shuanlgu.api;
import com.google.gson.Gson;
import com.yunfei.shuanlgu.result.ResultModel;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpStatus;
import java.util.Map;
/**
 * @program: app-shuanglu-servlet
 * @description:
 * @author: zhengyunfei
 * @create: 2019-08-08 16:03
 **/
public class UserInfoApiUtil {
    public ResultModel getUserInfo(String type,String token){
        ResultModel resultModel=new ResultModel();
        String apiUrl = "http://ai.shassel.com/app-shuanglu-center/api/v2/user/getShuangLuUserInfo";
        //测试环境
        if("test".equals(type)){
            apiUrl="http://aitest.shassel.com:8080/app-shuanglu-center/api/v2/user/getShuangLuUserInfo";
        }
        Gson gson = new Gson();
        HttpClient client = null;
        PostMethod method = null;
        try{
            client = new HttpClient();
            client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
            method = new PostMethod(apiUrl);
            //传参
            method.setParameter("token",token);
            //头文件
            method.addRequestHeader("Content-Type","application/x-www-form-urlencoded");
            int code = client.executeMethod(method);
            if(code!= HttpStatus.SC_OK){
                throw new Exception("");
            }
            String respText = method.getResponseBodyAsString();
            //返回的数据
             resultModel = gson.fromJson(respText, ResultModel.class);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(method!=null){
                method.releaseConnection();
            }
            if(client!=null){
                client.getHttpConnectionManager().closeIdleConnections(1);
            }
        }
        return resultModel;
    }
    public static void main(String args[]){

    }
}
